package com.simple.sso.sysa.config;

import com.simple.sso.client.filter.LoginFilter;
import com.simple.sso.client.filter.LogoutFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class SystemConfig {

    /**
     * 配置使用注销登录过滤器
     */
    @Bean
    public FilterRegistrationBean<LogoutFilter> logoutFilterFilterRegistrationBean() {
        FilterRegistrationBean<LogoutFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        LogoutFilter logoutFilter = new LogoutFilter();
        filterRegistrationBean.setFilter(logoutFilter);

        Map<String, String> initParamMap = new HashMap<>();

        //初始化参数中指定调用认证中心注销登录请求
        initParamMap.put("logoutUrl", "http://localhost:8080/server/logout");
        initParamMap.put("clientIndexUri", "/sysa/success");
        filterRegistrationBean.setInitParameters(initParamMap);
        return filterRegistrationBean;
    }

    /**
     * JavaConfig方式配置第三方过滤器，通过注入FilterRegistrationBean对象完成
     */
    @Bean
    public FilterRegistrationBean<LoginFilter> loginFilter() {
        FilterRegistrationBean<LoginFilter> filterBean = new FilterRegistrationBean<>();
        //sso-client模块中定义的统一登录过滤器，拦截所有的请求
        LoginFilter loginFilter = new LoginFilter();
        filterBean.setFilter(loginFilter);
        Map<String, String> initParamMap = new HashMap<>();
        initParamMap.put("loginUrl", "http://localhost:8080/server");
        initParamMap.put("checkTokenValidUrl", "http://localhost:8080/server/checkTokenValid");
        filterBean.setInitParameters(initParamMap);
        return filterBean;
    }
}
